package com.tb.attendancesystem.teacher;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.tb.attendancesystem.R;
import com.tb.attendancesystem.database.CourseBean;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.Call;

/**
 * 教师端主页面
 */

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    private Button btPrint;                     //输入二维码的按钮
    private Spinner courseSelector;             //课程选择
    private String tid;                         //使用者id
    private String selectCourseId;              //选择的课程ID
    private int selectCourseTimeCount;          //选择的课程点名次数


    //加载资源
    private void loadRes() {
        tid = getIntent().getStringExtra("userId");
        btPrint = (Button) findViewById(R.id.bt_print);
        courseSelector = (Spinner) findViewById(R.id.courses_selector);
    }

    //设置监听
    private void register() {
        btPrint.setOnClickListener(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_t_main);
        loadRes();
        register();
        getCoursesByTeacherId(tid);
    }

    //通过教师id获取课程数组并初始化Spinner
    private void getCoursesByTeacherId(String tid) {
        //访问服务器获取json
        OkHttpUtils.get().url(getResources().getString(R.string.host_url) + "/AttendanceSystem/GetCourseInfos")
                .addParams("tid", tid)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.d(MainActivity.class.getSimpleName(), response);
                        //存放获取到的CourseBean
                        ArrayList<CourseBean> list = new ArrayList<>();
                        //解析获取到的json数据
                        try {
                            JSONArray jsonArray = new JSONArray(response);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                CourseBean bean = new CourseBean(jsonObject.getString("cid"), jsonObject.getString("course_name"), jsonObject.getInt("time_count"));
                                list.add(bean);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        //初始化Spinner
                        initSpinner(list);
                    }
                });
    }

    //初始化Spinner
    private void initSpinner(final ArrayList<CourseBean> courseList) {
        //获取课程名称
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < courseList.size(); i++) {
            list.add(courseList.get(i).getCourseName());
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        courseSelector.setAdapter(adapter);
        courseSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.d(TAG, "position:" + position + "  --id:" + id);
                selectCourseId = courseList.get(position).getCid();
                selectCourseTimeCount = courseList.get(position).getTimeCount();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.equals(btPrint)) {
            Intent intent = new Intent(this, ResultActivity.class);
            intent.putExtra("cid", selectCourseId);
            intent.putExtra("times", selectCourseTimeCount + 1);
            startActivity(intent);
        }
    }
}
